package org.zjvis.datascience.common.widget.vo;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.zjvis.datascience.common.widget.WordGeneratorUtil;

import java.util.Arrays;
import java.util.Locale;
import java.util.Map;

import static org.zjvis.datascience.common.widget.constant.WordTemplateConstant.IMAGE_SNIPPET;

/**
 * @description TODO
 * @date 2022/2/15
 */
@Data
@ToString
public class WordImageVO {

    private Integer id;

    private String url;

    private String refId;

    private String imgName;

    private Integer imgWidth;//单位pixel

    private Integer imgHeight;//单位pixel

    private Long imgHeightInEMU;//word中单位是EMU

    private Long imgWidthInEMU;

    private NetImage imageObj;

    private String imgSuffix = "png";

    private static final String IMG_PREFIX = "img";

    private static final String[] IMG_TYPES = {"png", "jpeg", "bmp", "gif", "svg"};

    private static final Float imgWidthInCM = 10.5f;


    /**
     * https://unit-converter.kurylk.in/
     * cm -> inch -> EMU
     * @return
     */
    private Long getImgWidthInEMU(){
        return ((Float)(imgWidthInCM / 2.54f * 914400.0f)).longValue();
    }

    public WordImageVO(int i, String imgName) {
        this.setId(i);
        this.setRefId(IMG_PREFIX + i);
        this.setImgName(imgName);
    }

    public WordImageVO(int i, String imgName, String url) {
        this.setId(i);
        this.setRefId(IMG_PREFIX + i);
        this.setImgName(imgName);
        this.setUrl(url);
        this.extractSuffix();
    }


    private void extractSuffix() {
        String[] s = this.url.split("/");
        try {
            String suffix = s[s.length - 1].split("\\.")[1].toLowerCase(Locale.ROOT);
            this.setImgSuffix(isAppropriateType(suffix) ? suffix : "png");
        } catch (ArrayIndexOutOfBoundsException e) {
            this.setImgSuffix("png");
        }
    }

    private static boolean isAppropriateType(String imgSuffix) {
        return Arrays.asList(IMG_TYPES).contains(imgSuffix);
    }

    /**
     * 默认后缀是png
     *
     * @return
     */
    public String getFullName() {
        return this.imgName + "." + this.imgSuffix;
    }

    public String getBase64Code() {
        if (ObjectUtil.isNotNull(this.getImageObj())) {
            return this.getImageObj().getBase64Code();
        } else {
            NetImage netImage = WordGeneratorUtil.downloadImage(this.getUrl(), this.getImgSuffix());
            this.setImage(netImage);
            return netImage.getBase64Code();
        }
    }

    private void setImage(NetImage netImage) {
        this.setImageObj(netImage);
        this.setImgWidth(netImage.getWidth());
        this.setImgHeight(netImage.getHeight());
        this.computeImgHeightInEMU();
    }

    private void computeImgHeightInEMU() {
        this.setImgHeightInEMU(this.getImgWidthInEMU() * this.getImgHeight() / this.getImgWidth());
    }

    public String toXML(Integer index, String chartName) {
        Map<String, String> valuesMap = Maps.newHashMap();
        valuesMap.put("imgIndex", String.valueOf(index));
        valuesMap.put("imgId", String.valueOf(this.getId()));
        valuesMap.put("imgRefId", this.getRefId());
        valuesMap.put("imgName", this.getImgName());
        valuesMap.put("imgFootNote", "图" + this.getId() + ". " + chartName);
        valuesMap.put("imgWidth", String.valueOf(this.getImgWidthInEMU()));
        if (ObjectUtil.isNotNull(this.getImageObj())) {

        } else {
            NetImage netImage = WordGeneratorUtil.downloadImage(this.getUrl(), this.getImgSuffix());
            this.setImage(netImage);
        }
        valuesMap.put("imgHeight", String.valueOf(this.getImgHeightInEMU())); //默认插入图片的宽度是10.5cm 4.13inch 5952.755DXA 3780000EMU
        return new StrSubstitutor(valuesMap).replace(IMAGE_SNIPPET);
    }

    public String getImageType() {
        return "image/" + this.imgSuffix;
    }

    public static void main(String[] args) {
        WordImageVO asda = new WordImageVO(1, "ASDA", "https://t7.baidu.com/it/u=737555197,308540855&fm=193&f=GIF");
        System.out.println(asda.toString());
    }

}
